import base64
from rest_framework.views import APIView
from .models import Notices, About
from pytz import timezone

cst_tz = timezone('Asia/Shanghai')

class GetNoticeView(APIView):
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        notice = {
            'notice': [{
                'id': i.id,
                'title': i.title,
                'content': eval(repr(i.content).replace('\\\\', '\\')),
                'inner_web_url': eval(repr(i.inner_web_url).replace('\\\\', '\\')),
                'addition_action_title': i.addition_action_title,
                'addition_action_url': eval(repr(i.addition_action_url).replace('\\\\', '\\')),
                'background_color_1': i.background_color_1,
                'background_color_2': i.background_color_2,
                'background_color_3': i.background_color_3,
                'is_important': i.is_important,
                'date': i.date.astimezone(cst_tz).strftime("%Y-%m-%d %H:%M")
            } for i in Notices.objects.filter(is_show=True).all().order_by('-id')],
        }
        return notice
class GetLatestNoticeView(APIView):
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        cli_notice = Notices.objects.filter(is_show=True).all().order_by('-id').first()
        data = {
            'id': cli_notice.id,
            'title': cli_notice.title,
            'content': eval(repr(cli_notice.content).replace('\\\\', '\\')),
            'inner_web_url': eval(repr(cli_notice.inner_web_url).replace('\\\\', '\\')),
            'addition_action_title': cli_notice.addition_action_title,
            'addition_action_url': eval(repr(cli_notice.addition_action_url).replace('\\\\', '\\')),
            'background_color_1': cli_notice.background_color_1,
            'background_color_2': cli_notice.background_color_2,
            'background_color_3': cli_notice.background_color_3,
            'is_important': cli_notice.is_important,
            'date': cli_notice.date.astimezone(cst_tz).strftime("%Y-%m-%d %H:%M")
        }
        return data



class GetAboutView(APIView):
    # 无需认证
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        about = {
            "qa": [{
                "question": i.title,
                "answer": eval(repr(i.content).replace('\\\\', '\\')),
                "url": eval(repr(i.url).replace('\\\\', '\\'))
            } for i in About.objects.filter(type=1).all().order_by('dates')],
            "license": [{
                "title": j.title,
                "content": eval(repr(j.content).replace('\\\\', '\\')),
                "url": eval(repr(j.url).replace('\\\\', '\\'))
            } for j in About.objects.filter(type=2).all().order_by('dates')],
            "donate": [k.title + "-" + k.content + "：" + (k.dates).astimezone(cst_tz).strftime("%Y-%m-%d") for k in
                       About.objects.filter(type=3).all().order_by('dates')],
            "aboutMe": [{
                "title": i.title,
                "content": eval(repr(i.content).replace('\\\\', '\\')),
                "url": eval(repr(i.url).replace('\\\\', '\\'))
            } for i in About.objects.filter(type=4).all().order_by('dates')],
            "contact": {
                "wechat": "Milk_ore",
                "email": "milk@mail.mapotofu.cn",
                "qq": "2510355993"
            }
        }
        return about








